/*************************************************************
 * Title: ResourceSupport.java
 * Description: 
 * Author: Huang Shaobin
 * Email: shaobin.software@gmail.com
 * CreateTime: Feb 21, 2013 4:54:05 PM
 ************************************************************/
package snow.core.web.rest;

import java.util.Locale;

import javax.servlet.ServletContext;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import org.apache.commons.lang.ArrayUtils;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.restlet.Request;
import org.restlet.Response;
import org.restlet.data.Form;
import org.restlet.data.Reference;
import org.restlet.ext.servlet.ServletUtils;
import org.restlet.representation.EmptyRepresentation;
import org.restlet.representation.Representation;
import org.springframework.beans.factory.InitializingBean;
import org.springframework.context.MessageSource;

import snow.core.context.AccessContext;
import snow.core.domain.Result;
import snow.core.web.SystemParamService;

/**
 * REST资源父类
 */
public abstract class ResourceSupport implements InitializingBean {

	protected final Log log = LogFactory.getLog(getClass());

	private MessageSource messageSource;

	private SystemParamService systemParamService;

	public void afterPropertiesSet() throws Exception {
	}

	protected Representation getCurrentEntity() {
		Request request = Request.getCurrent();
		log.debug("当前Request对象为 -> " + request);
		Representation entity = request.getEntity();
		log.debug("当前的Representation对象为 -> " + entity);
		return entity;
	}

	protected Locale getCurrentLocale() {
		Locale locale = getRequest().getLocale();
		return locale == null ? Locale.getDefault() : locale;
	}

	public MessageSource getMessageSource() {
		return this.messageSource;
	}

	protected Form getPostAsForm() {
		Representation entity = getCurrentEntity();
		if (entity instanceof EmptyRepresentation) {
			return new Form();
		}
		return new Form(entity);
	}

	protected Form getQueryAsForm() {
		Request request = Request.getCurrent();
		log.debug("当前Request对象为 -> " + request);
		Reference reference = request.getResourceRef();
		log.debug("当前Reference对象为 -> " + reference);
		return reference.getQueryAsForm();
	}

	protected HttpServletRequest getRequest() {
		return ServletUtils.getRequest(Request.getCurrent());
	}

	protected HttpServletResponse getResponse() {
		return ServletUtils.getResponse(Response.getCurrent());
	}

	protected Result getResultFrom(boolean success, String code, Object message) {
		return new Result(success, code, message) {

			/**
			 * 
			 */
			private static final long serialVersionUID = -7847775862528130865L;

			public Object getMessage() {
				Object message = super.getMessage();
				if (message instanceof String) {
					if (messageSource != null) {
						try {
							final String i18n = messageSource.getMessage((String) message, ArrayUtils.EMPTY_OBJECT_ARRAY, getCurrentLocale());
							if (log.isInfoEnabled()) {
								log.info("当前请求的I18N值为：" + i18n);
							}
							message = i18n;
						} catch (Exception e) {
							log.warn(e.getMessage(), e);
						}
					}
				}
				return message;
			}
		};
	}

	protected ServletContext getServletContext() {
		return getSession().getServletContext();
	}

	protected HttpSession getSession() {
		return getRequest().getSession();
	}

	protected Result getSuccessResult() {
		return getSuccessResultFrom("success.operation");
	}

	protected Result getSuccessResultFrom(Object message) {
		return getResultFrom(true, "000000", message);
	}

	public SystemParamService getSystemParamService() {
		return this.systemParamService;
	}

	protected String getValueFrom(String name, String type, String defaultValue) {
		return systemParamService.getValueFrom(name, type, defaultValue);
	}

	protected void putSessionValueFrom(String name, Object value) {
		HttpSession session = getSession();
		session.setAttribute(name, value);
		AccessContext context = AccessContext.getCurrent();
		context.putFromSession(name, value);
	}

	protected void removeSessionValueFrom(String name) {
		HttpSession session = getSession();
		session.removeAttribute(name);
		AccessContext context = AccessContext.getCurrent();
		context.removeFromSession(name);
	}

	public void setMessageSource(MessageSource messageSource) {
		this.messageSource = messageSource;
	}

	public void setSystemParamService(SystemParamService systemParamService) {
		this.systemParamService = systemParamService;
	}
}
